package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.R;
import st.business.tepth.config.Url;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * @author zhangyu
 * @date 2019/5/13
 */
public class DialogAllTrouble extends Dialog {
    private Disposable mDisposable;

    public DialogAllTrouble(Context context, int repairId) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_check_result, null);
        TextView tvTitle = view.findViewById(R.id.tv_choose_action);
        tvTitle.setText("全部报修项");
        final RecyclerView recyclerView = view.findViewById(R.id.recycler_view_result);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        final ResultAdapter adapter = new ResultAdapter(R.layout.item_all_trouble, null);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        adapter.setEmptyView(R.layout.view_loading, (ViewGroup) recyclerView.getParent());
        mDisposable = RxRestClient.builder()
                .url(Url.GetAllTroubleByTask)
                .params("token", UserManager.getInstance().getToken())
                .params("repairId", repairId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        List<Trouble> list =
                                new ObjConverter<List<Trouble>>().convert(response
                                        , new TypeReference<List<Trouble>>() {
                                        });
                        adapter.setEmptyView(R.layout.view_search_empty,
                                (ViewGroup) recyclerView.getParent());
                        adapter.setNewData(list);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                })
                .build()
                .post();
        setContentView(view);
    }

    private class ResultAdapter extends BaseQuickAdapter<Trouble, BaseViewHolder> {

        public ResultAdapter(int layoutResId, @Nullable List<Trouble> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Trouble item) {
            helper.setText(R.id.tv_classification, item.getSeriesName());
            helper.setText(R.id.tv_trouble, item.getFaultName());
        }
    }

    public static class Trouble {
        private String seriesName;
        private String faultName;

        public String getSeriesName() {
            return seriesName;
        }

        public void setSeriesName(String seriesName) {
            this.seriesName = seriesName;
        }

        public String getFaultName() {
            return faultName;
        }

        public void setFaultName(String faultName) {
            this.faultName = faultName;
        }
    }

    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.dismiss();
    }
}
